<template>
  <div class="app-container calendar-list-container">
    <el-form :model="form" ref="form" :rules="rulesShops" label-width="100px">
      <el-form-item label="店铺名称" prop="shopName">
        <el-input v-model="form.shopName" placeholder="请输入店铺名称"></el-input>
      </el-form-item>

      <el-form-item label="联系人" prop="linkMan">
        <el-input v-model="form.linkMan" placeholder="请输入店铺联系人"></el-input>
      </el-form-item>

      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入店铺手机号码"></el-input>
      </el-form-item>

      <el-form-item label="店铺门头*" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          name="imageFile"
          :headers="requestHeader"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :data="uploadParam"
        >
          <img v-if="form.shopsImg" :src="imgBaseURL +form.shopsImg" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过2MB</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入店铺地址"></el-input>
      </el-form-item>

      <el-form-item label="支付方式" prop="regionPlay">
        <el-select placeholder="支付方式" v-model="region" @change="selectTrigger(region)">
          <el-option
            v-for="item in paramPicklist"
            :key="item.picklistValue"
            :label="item.picklistText"
            :value="item.picklistValue"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="支付单位" prop="type">
        <el-checkbox-group v-model="type">
          <el-checkbox
            v-for="payUnit in readyUplist"
            :label="payUnit.picklistValue"
            :key="payUnit.picklistValue"
          >{{payUnit.picklistText}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="分期" prop="typee">
        <el-checkbox-group v-model="typee">
          <el-checkbox label="6" name="typee" :disabled="statsCheckbox">6期</el-checkbox>
          <el-checkbox label="12" name="typee" :disabled="statsCheckbox">12期</el-checkbox>
          <el-checkbox label="24" name="typee" :disabled="statsCheckbox">24期</el-checkbox>
          <el-checkbox label="36" name="typee" :disabled="statsCheckbox">36期</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <div class="dialog-footer">
        <el-button @click="closeShopShops" id="shopJuLiS">取 消</el-button>
        <el-button type="primary" @click="addShopShops('form')" id="shopsJuLi">确 定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getAllPicks } from "api/shop/shops/index";
import { getPayingUnit } from "api/shop/shops/index";
import { getPayingStaging } from "api/shop/shops/index";
import { mapGetters } from "vuex";
import { addShopShops } from "api/shop/shops/index";
import { getToken } from "utils/auth";
export default {
  name: "shopManage",
  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        shopName: undefined
      },
      tableKey: 0,
      dialongVisible: false, //
      form: {
        shopName: undefined,
        linkMan: undefined,
        mobile: undefined,
        address: undefined,
        regionPlay: undefined,
        typePlay: [],
        typeePlay: [],
        shopsImg: undefined
      },
      paramPicklist: [], //支付方式
      region: [], //支付方式的遍历值
      regionValue: undefined, //支付方式的值
      statsCheckbox: true, //分期支付日期是否可选
      type: [], //支付单位
      typee: [], //分期支付
      readyUplist: [], //获取后台的支付单位
      requestHeader: { token: getToken() },
      uploadImgUrl: this.GLOBAL.uploadImgUrl,
      imgBaseURL: this.GLOBAL.imgBaseURL,
      uploadParam: { useType: "shop" },
      rulesShops: {
        shopName: [
          {
            required: true,
            message: "请输入店铺名称",
            trigger: "blur"
          }
        ],
        linkMan: [
          {
            required: true,
            message: "请输入店铺联系人",
            trigger: "blur"
          }
        ],
        mobile: [
          {
            required: true,
            message: "请输入店铺手机号码",
            trigger: "blur"
          }
        ],
        address: [
          {
            required: true,
            message: "请输入店铺地址",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getParamPicklist();
  },
  computed: {
    ...mapGetters(["elements"])
  },
  methods: {
    getParamPicklist() {
      getAllPicks().then(response => {
        this.paramPicklist = response.data;
      });
    },
    selectTrigger(val) {
      this.regionValue = val;
      if (val == 1) {
        this.statsCheckbox = true;
        getPayingUnit().then(response => {
          this.readyUplist = response.data;
        });
      } else if (val == 10) {
        this.statsCheckbox = false;
        getPayingStaging().then(response => {
          this.readyUplist = response.data;
        });
      }
    },
    handleAvatarSuccess(res, file) {
      this.form.shopsImg = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error("上传图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return (isJPG || isPNG) && isLt2M;
    },
    addShopShops(form) {
      var val = this.regionValue;
      if (this.form.shopsImg == "" || this.form.shopsImg == null) {
        this.$message.error("店铺门头不能为空");
        return false;
      } else if (val == "") {
        this.$message.error("店铺支付方式不能为空");
        return false;
      } else {
        const set = this.$refs;
        set[form].validate(valid => {
          if (valid) {
            if (this.type != "") {
              this.form.typePlay = this.type;
            }
            if (this.typee != "") {
              this.form.typeePlay = this.typee;
            }
            this.form.regionPlay = val;
            addShopShops(this.form).then(response => {
              if (response.status == 200) {
                this.$notify({
                  title: "成功",
                  message: "添加成功!",
                  type: "success",
                  duration: 2000
                });
                this.$router.push("/shopManage/shopsManage");
              } else {
                this.$router.push("/shopManage/shopsManage");
              }
            });
          } else {
            this.$notify({
              title: "失败",
              message: "提交失败!",
              type: "error",
              duration: 2000
            });
          }
        });
      }
    },
    closeShopShops() {
      this.$router.push("/shopManage/shopsManage");
    }
  }
};
</script>
<style>
#shopJuLiS {
  margin-left: 70px;
}
#shopsJuLi {
  margin-left: 350px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
